//#include "MemoryPool.h"
//#include "Concurrent.h"
//#include "Log.h"
//
//
//void TestAlloc1()
//{
//	for (int i = 0; i < 5; i++)
//	{
//		void* ptr = ConcurrentAlloc(10);
//	}
//}
//
//void TestAlloc2()
//{
//	for (int i = 0; i < 5; i++)
//	{
//		void* ptr = ConcurrentAlloc(20);
//	}
//}
//
//void Test()
//{
//	//LogModule::Log(LogLevel::)
//	//LOG(INFO) << "这是一个日志信息";
//	//LOG(ERROR) << "发生了错误，变量x=";
//
//	std::thread t1(TestAlloc1);
//	std::thread t2(TestAlloc2);
//	t1.join();
//	t2.join();
//}
//
//int main()
//{
//	//TestObjectPool();
//	//Test();
//	std::cout << sizeof(void*) << std::endl;
//	return 0;
//}
//
//


#include "MemoryPool.h"
#include "Concurrent.h"
//#include "Log.h"

//Assertion failed: span, file D:\codeofc++msvc\Project\project-c\HighCorruentMemoryPool\HighCorruentMemoryPool\CentralCache.cpp, line 21
// 这里的函数还没有实现完全，运行下面的测试程序时程序崩了
// 测试大小为size的内存分配
void TestSizeClassAlloc(size_t size)
{
    //std::cout << "开始测试大小为 " << size << " 字节的内存分配";

    std::vector<void*> v;
    for (int i = 0; i < 10; ++i) {
        std::cout << "hello0\n";

        void* ptr = ConcurrentAlloc(size);
        std::cout << "hello1\n";
        std::cout << "分配第 " << i + 1 << " 个内存块，地址: " << ptr << std::endl;
        v.push_back(ptr);
    }

    // 释放所有分配的内存
    for (void* ptr : v) {
        ConcurrentDealloc(ptr, size);
        std::cout << "释放内存块，地址: " << ptr << std::endl;
    }
}

// 多线程测试不同大小的内存分配
void TestMultiThreadAlloc()
{
    std::cout << "开始多线程内存分配测试" << std::endl;

    std::vector<std::thread> threads;
    std::vector<size_t> sizes = { 10, 20, 100, 200, 1000, 4000, 8000 }; // 测试不同的内存大小

    for (size_t size : sizes) {
        threads.push_back(std::thread([size]() {
            //std::cout << "线程 " << std::this_thread::get_id() << " 开始测试" << std::endl;
            TestSizeClassAlloc(size);
            //std::cout << "线程 " << std::this_thread::get_id() << " 测试完成" << std::endl;
            }));
    }

    // 等待所有线程完成
    for (auto& t : threads) {
        t.join();
    }
}

void TestThreadCacheAndCentralCache()
{
    //Enable_Console_Sync_Strategy; // 启用控制台日志输出
    std::cout << "开始测试 ThreadCache 和 CentralCache" << std::endl;

    // 1. 测试单线程小内存分配
    std::cout << "测试单线程小内存分配" << std::endl;
    TestSizeClassAlloc(8);
    std::cout << "hello\n";

    TestSizeClassAlloc(16);

    // 2. 测试单线程大内存分配
    std::cout << "测试单线程大内存分配" << std::endl;
    TestSizeClassAlloc(4096);
    TestSizeClassAlloc(8192);

    // 3. 测试多线程并发分配
    //std::cout << "测试多线程并发分配" << std::endl;
    TestMultiThreadAlloc();

    //std::cout << "所有测试完成" << std::endl;
}

int main()
{
    //TestThreadCacheAndCentralCache();
    //void* p1 = ConcurrentAlloc(6);
    //
    //void* p2 = ConcurrentAlloc(8);
    //
    //void* p3 = ConcurrentAlloc(10);

    ////
    //void* p4 = ConcurrentAlloc(7);

    //void* p5 = ConcurrentAlloc(1000);
    //std::cout << p1 << std::endl;
    //std::cout << p2 << std::endl;
    //std::cout << p3 << std::endl;
    //std::cout << p4 << std::endl;
    //std::cout << p5 << std::endl;
    //ConcurrentDealloc(p2, 8);
    //ConcurrentDealloc(p1, 6);
    //ConcurrentDealloc(p4, 7);
    //ConcurrentDealloc(p3, 10);
    //ConcurrentDealloc(p5, 1000);
    //std::vector<void*> ptr(100, nullptr);
    //std::vector<int> arr(100, -1);
    //for (int i = 0; i < 100; i++)
    //{
    //    arr[i] = rand() % 100 + 4;
    //}
    //for (int i = 0; i < 100; i++)
    //{
    //    ptr[i] = ConcurrentAlloc(arr[i]);//[4, 103]
    //    std::cout << "ptr[" << i << "]: " << ptr[i] << std::endl;
    //    if (i % 5 == 0)
    //    {
    //        ConcurrentDealloc(ptr[i], arr[i]);
    //    }
    //}
    for (int i = 0; i < 10; i++)
    {
        std::cout << "第" << i << "次分配" << ConcurrentAlloc(8) << std::endl;
    }
    return 0;
}